package com.example.tys.baseapplication.DataModel.greendao;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.tys.baseapplication.DataModel.data.IDaoOperation;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

/**
 * @author tys
 * @date 2017/9/30
 */

class GreenDaoComOperation<DataTypeDao extends AbstractDao<DataType, ?>, DataType> implements
        IDaoOperation<DataType> {
    private final DataTypeDao dao;

    GreenDaoComOperation(DataTypeDao dao) {
        this.dao = dao;
    }

    @Override
    public long insert(@NonNull DataType obj) {
        return dao.insert(obj);
    }

    @Override
    public void insert(@NonNull List<DataType> objs) {
        dao.insertInTx(objs);
    }

    @Override
    public List<DataType> query(@Nullable String andOr, @Nullable Object[] whereArgs, @Nullable Integer offset,
                                @Nullable Integer limit, @Nullable String order, @Nullable Object... filed) {
        QueryBuilder<DataType> mBuilder = dao.queryBuilder();
        if (limit != null) {
            mBuilder.limit(limit);
        }
        if (offset != null) {
            mBuilder.offset(offset);
        }
        if (!TextUtils.isEmpty(order) && filed != null) {
            if (order.equalsIgnoreCase("ASC")) {
                mBuilder.orderAsc((Property[]) filed);
            }
            if (order.equalsIgnoreCase("DESC")) {
                mBuilder.orderDesc((Property[]) filed);
            }
        }
        if (whereArgs != null) {
            if (whereArgs.length == 1) {
                mBuilder.where((WhereCondition) whereArgs[0]);
            } else {
                if (TextUtils.isEmpty(andOr)) {
                    throw new IllegalArgumentException("andOr is not null");
                }
                WhereCondition where = null;
                if (andOr.equalsIgnoreCase("AND")) {
                    if (whereArgs.length == 2) {
                        where = mBuilder.and((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1]);
                    } else if (whereArgs.length == 3) {
                        where = mBuilder.and((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1], (WhereCondition) whereArgs[2]);
                    } else if (whereArgs.length == 4) {
                        where = mBuilder.and((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1], (WhereCondition) whereArgs[2], (WhereCondition) whereArgs[3]);
                    }
                } else {
                    if (whereArgs.length == 2) {
                        where = mBuilder.or((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1]);
                    } else if (whereArgs.length == 3) {
                        where = mBuilder.or((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1], (WhereCondition) whereArgs[2]);
                    } else if (whereArgs.length == 4) {
                        where = mBuilder.or((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1], (WhereCondition) whereArgs[2], (WhereCondition) whereArgs[3]);
                    }
                }
                mBuilder.where(where);
            }
        }
        return mBuilder.build().list();
    }

    @Override
    public DataType uniqueQuery(@Nullable String andOr, @NonNull Object[] whereArgs) {
        QueryBuilder<DataType> mBuilder = dao.queryBuilder();
        if (whereArgs.length == 1) {
            mBuilder.where((WhereCondition) whereArgs[0]);
        } else {
            if (TextUtils.isEmpty(andOr)) {
                throw new IllegalArgumentException("and_or is null");
            }
            WhereCondition where = null;
            if (andOr.equalsIgnoreCase("AND")) {
                if (whereArgs.length == 2) {
                    where = mBuilder.and((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1]);
                } else if (whereArgs.length == 3) {
                    where = mBuilder.and((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1], (WhereCondition) whereArgs[2]);
                } else if (whereArgs.length == 4) {
                    where = mBuilder.and((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1], (WhereCondition) whereArgs[2], (WhereCondition) whereArgs[3]);
                }
            } else {
                if (whereArgs.length == 2) {
                    where = mBuilder.or((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1]);
                } else if (whereArgs.length == 3) {
                    where = mBuilder.or((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1], (WhereCondition) whereArgs[2]);
                } else if (whereArgs.length == 4) {
                    where = mBuilder.or((WhereCondition) whereArgs[0], (WhereCondition) whereArgs[1], (WhereCondition) whereArgs[2], (WhereCondition) whereArgs[3]);
                }
            }
            mBuilder.where(where);
        }
        return mBuilder.build().unique();
    }

    @Override
    public void update(@NonNull DataType newObj) {
        dao.update(newObj);
    }

    @Override
    public void delete(@NonNull DataType obj) {
        dao.delete(obj);
    }

    @Override
    public void deleteAll() {
        dao.deleteAll();
    }


}
